package com.ddf.jpa.boot.service;

import com.ddf.jpa.boot.entity.User;
import com.ddf.jpa.boot.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author DDf on 2018/10/4
 */
@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    @Transactional
    public User save(User user) {
        User exist = userRepository.getUserByUsernameAndPassword(user.getUsername(), user.getPassword());
        System.out.println("exist: " + exist);
        if (exist != null) {
            user.setId(exist.getId());
        }
        User rtn = userRepository.save(user);
        System.out.println("rtn: " + rtn);
        return rtn;
    }

    @Transactional
    public Page<User> findAll(Pageable pageRequest) {
        return userRepository.findAll(pageRequest);
    }
}
